package com.redlion.weapon;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.primitive.Line;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

import com.redlion.Game;

public class LaserStroke extends Line {

	private static float outerFactor = 5;
	private static float centerFactor = 2;
	private Color innerColor = new Color(0.9f, 0.9f, 1);
	private Color centerColor = new Color(0.7f, 0.7f, 0.3f);
	public static final float maxTime = 0.5f;
	private static final float normalizer = 1f / maxTime;
	Line innerLine;
	Line centerLine;
	IUpdateHandler handler;

	public LaserStroke(float pX1, float pY1, float pX2, float pY2, final float pLineWidth,
			VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX1, pY1, pX2, pY2, pLineWidth * outerFactor, pVertexBufferObjectManager);
		handler = new IUpdateHandler() {
			float sum = maxTime;
			private float normalized;

			@Override
			public void reset() {
				Game.pools.laserStrokePool.recyclePoolItem(LaserStroke.this);
				sum = maxTime;
			}

			@Override
			public void onUpdate(float pSecondsElapsed) {
				sum -= pSecondsElapsed;
				if (sum < 0)
					reset();

				normalized = normalizer * sum;
				// LaserStroke.this.setLineWidth(pLineWidth * normalized *
				// outerFactor);
				// centerLine.setLineWidth(pLineWidth * normalized *
				// centerFactor);
				// innerLine.setLineWidth(pLineWidth * normalized);
				setLineWidth(pLineWidth * normalized);
				LaserStroke.this.setAlpha(normalized * 0.8f);
				innerLine.setAlpha(normalized * 0.8f);
				centerLine.setAlpha(normalized * 0.8f);
			}
		};
		createInnerLines(0, 0, pX2 - pX1, pY2 - pY1, pLineWidth, pVertexBufferObjectManager);
		registerUpdateHandler(handler);
	}

	private void createInnerLines(float pX1, float pY1, float pX2, float pY2, float pLineWidth,
			VertexBufferObjectManager pVertexBufferObjectManager) {
		centerLine = new Line(pX1, pY1, pX2, pY2, pLineWidth * centerFactor, pVertexBufferObjectManager);
		centerLine.setColor(centerColor);
		innerLine = new Line(pX1, pY1, pX2, pY2, pLineWidth, pVertexBufferObjectManager);
		innerLine.setColor(innerColor);
		this.attachChild(centerLine);
		this.attachChild(innerLine);
	}

	@Override
	public void setPosition(float pX1, float pY1, float pX2, float pY2) {
		super.setPosition(pX1, pY1, pX2, pY2);
		centerLine.setPosition(0, 0, pX2 - pX1, pY2 - pY1);
		innerLine.setPosition(0, 0, pX2 - pX1, pY2 - pY1);
	}

	@Override
	public void setLineWidth(float pLineWidth) {
		super.setLineWidth(pLineWidth * outerFactor);
		centerLine.setLineWidth(pLineWidth * centerFactor);
		innerLine.setLineWidth(pLineWidth);
	}

	public void activate() {
		setIgnoreUpdate(false);
		setVisible(true);
		registerUpdateHandler(handler);
		setUserData(getClass());
	}

	public void deactivate() {
		setIgnoreUpdate(true);
		setVisible(false);
		setUserData(null);
		unregisterUpdateHandler(handler);
	}
}
